<template>
    <div style="min-height: 400px">
        <br/>
        <BasicForm @register="registerForm"></BasicForm>
        <BizPurchaseRequisitionDetailList v-if="reqId" :req-id="reqId"/>
    </div>
</template>

<script lang="ts" setup>
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {computed, defineComponent, defineProps, onMounted, ref, watch} from 'vue';
    import { propTypes } from '/@/utils/propTypes';
    import {formSchema} from '/@/views/business/purchase/biz_purchase_requisition/BizPurchaseRequisition.data';
    import {saveOrUpdate,detail} from '/@/views/business/purchase/biz_purchase_requisition/BizPurchaseRequisition.api';
    import {useMessage} from "@/hooks/web/useMessage";
    import BizPurchaseRequisitionDetailList from "@/views/business/purchase/biz_purchase_requisition_detail/BizPurchaseRequisitionDetailList.vue";

    const { createMessage} = useMessage()

    const emits = defineEmits(['ok','close'])
    const reqId = ref(null)
    const props = defineProps({
      formData: propTypes.object.def({}),
      formDisabled:propTypes.bool.def(false),
      processData:propTypes.object.def({}),
      isNew:propTypes.bool.def(true),
      dataSet:propTypes.bool.def(false),
      withStore:propTypes.bool.def(false)
    })

    const formDisabled = computed(()=>{
        return props.formData.disabled !== false;

    });

    watch(()=>props.formData,
    (val)=>{
      if (val.tableId){
        reqId.value = val.tableId
      }
    },
      {deep: true}
    )

     const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 12}
     });

    async function initFormData(){
      await resetFields();
      reqId.value = null
      if (!props.isNew){
        reqId.value = props.formData.tableId
        const temp = await detail({id:props.formData.tableId})
        await setFieldsValue({
            ...temp,
        });
      }else {
        if (props.formData?.tableData){
          await setFieldsValue({
              ...props.formData.tableData,
          });
        }
      }

    }

     //审批回调
    async function submitForm(callBack:Function) {
        let svData = await validate();
        if (props.withStore){
            const res = await saveOrUpdate(svData, !props.isNew);
            await setFieldsValue({
              id:res
            })
          svData.id = res
          callBack(svData)
        }else {
          callBack(svData)
        }
    }
    //数据保存回调
    async function saveForm(callBack:Function){
      let svData = await validate();
      const res = await saveOrUpdate(svData, !props.isNew);
      await setFieldsValue({
        id:res
      })
      callBack(res)
    }

    onMounted(()=>{
      initFormData();
    })

    defineExpose({
      submitForm,
      saveForm
    })
</script>
